public class CloneTest implements Cloneable{
    private String name;
    private Age age;
 
    public CloneTest(String name, Age age) {
        this.name = name;
        this.age = age;
    }
 
    public String getName() {
        return name;
    }
 
 
 
    @Override
    protected Object clone() throws CloneNotSupportedException {
        /**
         * 浅拷贝
         */
//        return super.clone();
 
        /**
         * 深拷贝
         */
        CloneTest clone = (CloneTest)super.clone();
        clone.age = (Age) age.clone();

        return clone;
    }
 
    @Override
    public String toString() {
        return "CloneTest{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class Age implements Cloneable {
    private int years;

    public Age(int years) {
        this.years = years;
    }


    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public String toString() {
        return "Age{" +
                "years=" + years +
                '}';
    }

    public static void main(String[] args) {
        //浅克隆
//        Age age = new Age(23);
//        CloneTest cloneTest = new CloneTest("老铁", age);
//        try {
//            CloneTest clone = (CloneTest)cloneTest.clone();
//            //首次输出
//            System.out.println(clone);
//            age.years = 12;
//            //二次输出，更改引用数据类型的值后输出。如果是浅拷贝首次输出和二次输出将是不一样的值，深拷贝者会相同
//            System.out.println(clone);
//        } catch (CloneNotSupportedException e) {
//            e.printStackTrace();
//        }
        //深克隆
        Age age = new Age(23);
        CloneTest cloneTest1 = new CloneTest("小子", age);
        try {
            CloneTest cloneTest = (CloneTest) cloneTest1.clone();
            //首次输出
            System.out.println(cloneTest);
            age.years = 12;
            //二次输出，更改引用数据类型的值后输出。如果是浅拷贝首次输出和二次输出将是不一样的值，深拷贝者会相同
            System.out.println(cloneTest);
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }


    }
}
